package com.xbfinal.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbfinal.reggie.common.R;
import com.xbfinal.reggie.dto.OrdersDto;
import com.xbfinal.reggie.dto.SetmealDto;
import com.xbfinal.reggie.pojo.Category;
import com.xbfinal.reggie.pojo.Orders;
import com.xbfinal.reggie.pojo.Setmeal;
import com.xbfinal.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 个人订单查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page ,int pageSize){


        Page pageInfo = new Page(page, pageSize);//前端传过来分页的当前码和分页的每一页的大小
        LambdaQueryWrapper<Orders> queryWrapper
                =new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 查询订单详细
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page> listPage(int page ,int pageSize){
        Page pageInfo = new Page(page, pageSize);//前端传过来分页的当前码和分页的每一页的大小
        Page OrdersInfo=new Page(page, pageSize);

        LambdaQueryWrapper<Orders> queryWrapper
                =new LambdaQueryWrapper<>();

        queryWrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,OrdersInfo);
        final List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list=records.stream().map(item->{
            OrdersDto ordersDto=new OrdersDto();
            final Long userId = item.getId();
            BeanUtils.copyProperties(item,ordersDto);

            //根据分类的id查询
            final Orders byId = orderService.getById(userId);
            if(byId!=null){
                //getConsignee是下单用户
                ordersDto.setUserName(byId.getConsignee());
            }
            return ordersDto;
        }).collect(Collectors.toList());
        OrdersInfo.setRecords(list);
        return R.success(OrdersInfo);

    }


    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders){

        //查看id
        LambdaQueryWrapper<Orders> queryWrapper
                =new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getId,orders.getId());

        //修改对应id的状态
        final Orders one = orderService.getOne(queryWrapper);
        one.setStatus(orders.getStatus());
        orderService.updateById(one);
        return R.success("完成");
    }
}